<template>
  <div class="container-fluid">
    <!--搜索框-->
    <div style="margin-top: 10px">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-row>
          <el-col :span="20">
            <el-input
              v-model="formInline.user"
              placeholder="请输入用户名"
            ></el-input>
          </el-col>
          <el-col :span="2">
            <el-form-item>
              <el-button type="primary" @click="onSubmit">查询用户</el-button>
            </el-form-item>
          </el-col>

          <el-col :span="2">
            <el-form-item>
              <el-button type="primary" @click="addDialogVisible = true"
                >添加用户</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <!--用户列表 -->
    <div>
      <el-table :data="list" style="width: 100%" border>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="id" label="编号" width="180"> </el-table-column>
        <el-table-column prop="username" label="工号" width="180">
        </el-table-column>
        <el-table-column prop="password" label="密码"> </el-table-column>
        <el-table-column prop="name" label="姓名"> </el-table-column>
        <el-table-column prop="sex" label="性别">
          <template slot-scope="scope">
            <span v-if="scope.row.sex == 1">男</span>
            <span v-if="scope.row.sex == 0">女</span>
          </template>
        </el-table-column>
        <el-table-column prop="phone" label="电话"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click.native.prevent="deleteRow(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!--分页-->

    <!--添加用户的对话框-->
    <el-dialog
      title="添加用户"
      :visible.sync="addDialogVisible"
      width="50%"
      @close="addDialogClosed"
    >
      <!--主体区域-->
      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="70px"
      >
        <el-form-item label="编号" prop="id">
          <el-input v-model="addForm.id"></el-input>
        </el-form-item>
        <el-form-item label="工号" prop="username">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio v-model="addForm.sex" label="1">男</el-radio>
          <el-radio v-model="addForm.sex" label="0">女</el-radio>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="addForm.phone"></el-input>
        </el-form-item>
      </el-form>
      <!--底部区域-->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    //验证手机号
    var checkPhone = (rule, value, cb) => {
      const regPhone =
        /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;
      if (regPhone.test(value)) {
        return cb();
      }
      cb(new Error("请输入合法手机号"));
    };
    return {
      drawer: false,
      formInline: {
        user: "",
        region: "",
      },
      //隐藏添加框
      addDialogVisible: false,
      //定义添加框内数据
      addForm: {
        id: "",
        username: "",
        password: "",
        name: "",
        sex: "",
        phone: "",
      },
      //定义添加规则
      addFormRules: {
        username: [
          {
            required: true,
            message: "请输入工号",
            trigger: "blur",
          },
          {
            min: 3,
            max: 10,
            message: "工号的长度在3-10个字符之间",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
          {
            min: 6,
            max: 15,
            message: "密码的长度在6-15个字符之间",
            trigger: "blur",
          },
        ],
        name: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur",
          },
          {
            min: 2,
            max: 10,
            message: "用户名的长度在2-10个字符之间",
            trigger: "blur",
          },
        ],
        phone: [
          {
            required: true,
            message: "请输入手机",
            trigger: "blur",
          },
          {
            validator: checkPhone,
            trigger: "blur",
          },
        ],
      },
      list: [],
      users: [],
    };
  },
  created() {
    this.query();
  },
  methods: {
    //删除
    deleteRow(id, row) {
      let url = `http://localhost:3000/users/` + row.id;
      this.axios.delete(url).then((response) => {
        console.log(response.data);
        this.list = response.data;
      });
      this.$confirm("此操作将删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then((response) => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.query();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //查询
    onSubmit() {
      this.axios(
        "http://localhost:3000/users?name=" + this.formInline.user
      ).then((response) => {
        console.log(response);
        if (response.length != 0) {
          console.log(response.data);
          this.list = response.data;
        }
      });
    },
    //添加数据
    async userStateChanged(userInfo) {
      if (res.meta.status !== 200) {
        userInfo.mg_state = !userInfo.mg_state;
        return this.$message.error(res.meta.msg);
      }
      this.$message.success(res.meta.msg);
    },
    // 监听添加用户对话框的关闭事件
    async addDialogClosed() {
      // 重置表单
      this.$refs.addFormRef.resetFields();
    },
    // 点击按钮，添加新用户
    addUser() {
        let url =`http://localhost:3000/users` ;
        this.axios.post(url,{
          id:this.addForm.id,
          username:this.addForm.username,
          password:this.addForm.password,
          name:this.addForm.name,
          sex:this.addForm.sex,
          phone:this.addForm.phone,
          content:this.formInline.content
        }).then((response)=>{
        console.log(response)
       });
      
        // 刷新用户列表
        this.query();
        // 隐藏弹出框
        this.addDialogVisible = false;
        // 刷新用户列表
        this.getUserList();
      
    },

    //调用数据
    query() {
      this.axios("http://localhost:3000/users").then((response) => {
        console.log(response.data);
        this.list = response.data;
      });
    },
  },
};
</script>



<style scoped>
</style>
